<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'

require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'

export default {
  name: 'Series',
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    title: {
      type: String,
      require: false
    },
    subtext: {
      type: String,
      require: false
    },
    float: {
      type: String,
      default: 'center'
    },
    legend: { // 侧边栏展示内容
      type: Array,
      require: false
    },
    selectedData: { // 默认展示的内容
      type: Array,
      require: false
    },
    seriesData: { // 统计项
      type: Array,
      require: true
    },
    radius: {
      type: String,
      default: '90%'
    },
    center: {
      type: Array,
      default: () => ['40%', '50%']
    }
  },
  data() {
    return {
      chart: null,
      legendData: []
    }
  },
  watch: {
    seriesData: {
      handler(newName, oldName) { // 值发生变化时
        this.initChart()
      },
      immediate: false, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: true // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  created() {
    if (this.legend !== null && undefined !== this.legend && this.legend.length > 0) {
      this.legendData = this.length
    } else {
      if (this.seriesData != null && this.seriesData.length > 0) {
        this.seriesData.forEach(data => {
          this.legendData.push(data.name)
        })
      }
    }
  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        title: {
          text: this.title, // 标题
          subtext: this.subtext, // 子标题
          left: this.float // 标题位置
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          type: 'scroll',
          orient: 'vertical',
          right: 10,
          top: 1,
          bottom: 1,
          data: this.legendData,
          selected: this.selectedData
        },
        series: [
          {
            name: this.title,
            type: 'pie',
            radius: this.radius,
            center: ['40%', '50%'],
            data: this.seriesData,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      })
    }
  }
}
</script>

<style scoped>

</style>
